<template>
  <div class="app-container">
    <!--对象搜索树型列表-->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="主机名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入主机名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="主机地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入主机IP"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主机状态" prop="status">
        <y-select
          v-model="queryParams.status"
          placeholder="请选择主机状态"
          :options="oms_host_status"
          clearable
        />
      </el-form-item>

      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--对象操作-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['oms:host:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleEdit"
          v-hasPermi="['oms:host:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oms:host:delete']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Upload"
          @click="handleImport"
          v-hasPermi="['oms:host:import']"
          >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <y-export
          :url="'oms/host/export/'"
          :ids="ids"
          :filename="'host_data'"
          v-hasPermi="['oms:host:export']"
        />
      </el-col>
      <y-right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      />
    </el-row>

    <!--对象列表-->
    <el-table
      ref="tableRef"
      v-loading="loading"
      row-key="id"
      :data="hostList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        reserve-selection
      />
      <el-table-column label="序号" width="100" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="主机编号"
        width="300"
        align="center"
        prop="id"
        v-if="columns[0].visible"
        sortable
      />
      <el-table-column
        label="主机名称"
        align="center"
        prop="name"
        v-if="columns[1].visible"
        :show-overflow-tooltip="true"
        sortable
      />
      <el-table-column
        label="主机地址"
        align="center"
        prop="address"
        v-if="columns[2].visible"
        :show-overflow-tooltip="true"
        sortable
      />
      <el-table-column
        label="操作系统"
        align="center"
        prop="os"
        v-if="columns[3].visible"
        sortable
      >
        <template #default="scope">
          <y-dict-tag :options="oms_oper_system" :value="scope.row.os" />
        </template>
      </el-table-column>
      <el-table-column
        label="主机类型"
        align="center"
        prop="type"
        v-if="columns[4].visible"
        sortable
      >
        <template #default="scope">
          <y-dict-tag :options="oms_host_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        v-if="columns[5].visible"
        sortable
      >
        <template #default="scope">
          <y-dict-tag :options="oms_host_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="create_time"
        v-if="columns[6].visible"
        sortable
        width="160"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" align="center" width="200">
        <template #default="scope">
          <el-tooltip content="释放" placement="top">
            <el-button
              link
              type="primary"
              icon="Refresh"
              @click="handleRelease(scope.row)"
              v-hasPermi="['oms:host:release']"
              :disabled="scope.row.status !== '1'"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['oms:host:edit']"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['oms:host:delete']"
            >
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!--列表分页-->
    <y-pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改主机对话框 -->
    <au-host
      :id="id"
      v-model="open"
      :title="title"
      :operate="operate"
      :getMethod="getHost"
      :addMethod="addHost"
      :updateMethod="updateHost"
      @submited="submited"
      @close="open = false"
    >
    </au-host>

    <!-- 导入对话框-->
    <y-import :upload="upload" @submited="submited" />
  </div>
</template>

<script setup name="HostManager">
import {
  listHost,
  getHost,
  delHost,
  addHost,
  updateHost,
  releaseHost,
} from "@/api/oms/host";
import AuHost from "./components/AuHost";

const { proxy } = getCurrentInstance();
const { oms_oper_system, oms_host_type, oms_host_status } = proxy.useDict(
  "oms_oper_system",
  "oms_host_type",
  "oms_host_status"
);

const id = ref(null);
const ids = ref([]);
const open = ref(false);
const title = ref("");
const total = ref(0);
const single = ref(true);
const loading = ref(true);
const multiple = ref(true);
const dateRange = ref([]);
const showSearch = ref(false);
const hostList = ref([]);
const operate = ref("add");

/*** 导入参数 */
const upload = reactive({
  // 是否显示弹出层
  open: false,
  // 弹出层标题
  title: "",
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/oms/host/import/",
});

const columns = ref([
  { key: 0, label: `主机编号`, visible: false },
  { key: 1, label: `主机名称`, visible: true },
  { key: 2, label: `主机IP`, visible: true },
  { key: 3, label: `操作系统`, visible: true },
  { key: 4, label: `登录类型`, visible: true },
  { key: 5, label: `状态`, visible: true },
  { key: 6, label: `创建时间`, visible: true },
]);

const data = reactive({
  queryParams: {
    name: undefined,
    status: undefined,
    page: 1,
    limit: 10,
  },
});

const { queryParams } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listHost(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      hostList.value = response.data;
      total.value = response.total;
      loading.value = false;
    }
  );
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  operate.value = "add";
  open.value = true;
  title.value = "添加主机";
}

/** 修改按钮操作 */
function handleEdit(row) {
  id.value = row.id || ids.value;
  operate.value = "update";
  open.value = true;
  title.value = "修改主机";
}

/** 释放按钮操作 */
async function handleRelease(row) {
  await releaseHost(row.id);
  getList();
  proxy.$modal.msgSuccess("释放成功");
}

/** 导入按钮操作 */
function handleImport() {
  upload.title = "用例导入";
  upload.open = true;
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    if (row.id) {
      await proxy.$modal.confirm(`确认是否删除 "${row.name}" ？`);
    } else {
      await proxy.$modal.confirm("确认是否删除所有已勾选信息？");
    }
    ids.value = row.id ? [row.id] : ids.value;
    await delHost(ids.value);
    proxy.$modal.msgSuccess("删除成功");
    proxy.$refs.tableRef.clearSelection();
    getList();
  } catch (error) {
    console.error(error);
  }
}

/** 添加/修改对象后触发事件 */
function submited() {
  getList();
}

onMounted(() => {
  getList();
});
</script>
